package hlhj.fhp.supreme.fragment

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebViewClient
import cn.sharesdk.framework.Platform
import cn.sharesdk.framework.PlatformActionListener
import cn.sharesdk.onekeyshare.OnekeyShare
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.ToastUtils
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.OptionDetailBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.option_text.view.*
import java.util.HashMap

/**
 * Created by Administrator on 2018\3\13 0013.
 */
class OptionDetailTextFgm :Fragment() {
    private lateinit var rootView:View
    companion object {
        fun newInstence(id:Int): OptionDetailTextFgm {
            LogUtil.logi("收到的Id$id")
            val textFgm = OptionDetailTextFgm()
            val bundle = Bundle()
            bundle.putInt("id",id)
            textFgm.arguments=bundle
            return textFgm
        }
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView=inflater.inflate(R.layout.option_text,null)
        val id = arguments!!.getInt("id")
        LogUtil.logi("收到的Id$id")
        rootView.webView.settings.javaScriptEnabled=true
        rootView.webView.webViewClient=object :WebViewClient(){

        }
        OkGo.post<OptionDetailBean>(Urls.BaseUrl+Urls.OPTIN_DETAIL)
                .params("id",id.toString())
                .execute(object :JsonCallBack<OptionDetailBean>(activity){
                    override fun onSuccess(response: Response<OptionDetailBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            rootView.tvTittle.text=bean?.data.title
//                            rootView.tvInfo.text=bean?.data.desc
//                            rootView.webView.loadDataWithBaseURL(null, "<p>3134</p><p>13413</p>", "text/html", "utf-8", null)
                            rootView.webView.loadDataWithBaseURL(null, bean?.data.content, "text/html", "utf-8", null)
                        }else{
                            MyUtils.toast(bean?.msg)
                        }
                    }
                })

        rootView.btShare.setOnClickListener {
            val listener=object : PlatformActionListener {
                override fun onComplete(p0: Platform?, p1: Int, p2: HashMap<String, Any>?) {
                    OkGo.post<BaseBean>(Urls.BaseUrl+Urls.SHARE)
                            .execute(object :JsonCallBack<BaseBean>(activity){
                                override fun onSuccess(response: Response<BaseBean>?) {
                                    val bean = response?.body()
                                    if (bean?.code==1){
                                        MyUtils.toast("分享成功")
                                    }else{
                                        MyUtils.toast(bean?.msg)
                                    }
                                }
                            })
                }

                override fun onCancel(p0: Platform?, p1: Int) {
                    ToastUtils.toast(context,"分享已取消")
                }

                override fun onError(p0: Platform?, p1: Int, p2: Throwable?) {
                    ToastUtils.toast(context,"分享失败")
                }
            }
            val oks = OnekeyShare()
            oks.callback=listener
            oks.setTitle("志尚课堂")
            // titleUrl是标题的网络链接，仅在Linked-in,QQ和QQ空间使用
            oks.setTitleUrl("https://www.baidu.com/")
            // text是分享文本，所有平台都需要这个字段
            oks.text = rootView.tvTittle.text.toString()
            //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
            oks.setImageUrl("http://zhishang.hlhjapp.com/static/images/logo.png")
            // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
            //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
            // url仅在微信（包括好友和朋友圈）中使用
            oks.setUrl("https://www.baidu.com/")
            // comment是我对这条分享的评论，仅在人人网和QQ空间使用
            oks.show(context)
        }
        return rootView
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        LogUtil.log("回来了？？？？")
        if (resultCode==-1){
            LogUtil.log("是否为空${data==null}")
        }
    }
}